package com.vdg.lockphotos.gridphotos;

import java.util.ArrayList;

import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.vdg.lockphotos.R;
import com.vdg.lockphotos.data.Album;
import com.vdg.lockphotos.notify.OnLoadFileListner;
import com.vdg.lockphotos.util.ImageLoader;
import com.vdg.lockphotos.util.Utilities;
import com.vdg.lockphotos.view.TextViewPlus;

public class ListAlbumsAdapter extends ArrayAdapter<Album> {
	private ArrayList<Album> mListAlbum;
	private Context mContext;
	private ImageLoader mImageLoader;
	private int mHeight;

	public ListAlbumsAdapter(Context context, int textViewResourceId,
			ArrayList<Album> objects) {
		super(context, textViewResourceId, objects);
		mListAlbum = objects;
		mContext = context;
		mImageLoader = ImageLoader.getInstance(getContext());
		mHeight = getHeight(mContext);
	}

	public static int getHeight(Context activity) {
		WindowManager wm = (WindowManager) activity
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		return (int) ((Utilities.getDisplaySize(display).x - Utilities.dpToPx(
				activity, 35)) / 4);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.album_row, parent, false);
			holder.mTxvTitle = (TextViewPlus) convertView
					.findViewById(R.id.txv_title);
			holder.mImv1 = (ImageView) convertView.findViewById(R.id.imv1);
			holder.mImv1.getLayoutParams().height = mHeight;
			holder.mImv2 = (ImageView) convertView.findViewById(R.id.imv2);
			holder.mImv2.getLayoutParams().height = mHeight;
			holder.mImv3 = (ImageView) convertView.findViewById(R.id.imv3);
			holder.mImv3.getLayoutParams().height = mHeight;
			holder.mImv4 = (ImageView) convertView.findViewById(R.id.imv4);
			holder.mImv4.getLayoutParams().height = mHeight;
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		Album album = mListAlbum.get(position);
		if (album != null) {
			if (album.getListPhotos().size() > 0) {
				holder.mImv1.setVisibility(View.VISIBLE);
				mImageLoader.DisplayImage(album.getListPhotos().get(0)
						.getImageUrl(), holder.mImv1, new OnLoadFileListner() {

					@Override
					public void onSuccess(boolean isAnimation) {
						// if (isAnimation) {
						// mImvPhoto.startAnimation(myFadeInAnimation);
						// }
					}

					@Override
					public void onStart() {
						// TODO Auto-generated method stub
						holder.mImv1.setImageDrawable(mContext.getResources()
								.getDrawable(R.drawable.white_drawable));
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						holder.mImv1.setImageDrawable(mContext.getResources()
								.getDrawable(R.drawable.white_drawable));
					}
				}, mHeight,true);
			}
			if (album.getListPhotos().size() > 1) {
				holder.mImv2.setVisibility(View.VISIBLE);
				mImageLoader.DisplayImage(album.getListPhotos().get(1)
						.getImageUrl(), holder.mImv2, new OnLoadFileListner() {

					@Override
					public void onSuccess(boolean isAnimation) {
						// if (isAnimation) {
						// mImvPhoto.startAnimation(myFadeInAnimation);
						// }
					}

					@Override
					public void onStart() {
						// TODO Auto-generated method stub
						holder.mImv2.setImageDrawable(mContext.getResources()
								.getDrawable(R.drawable.white_drawable));
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						holder.mImv2.setImageDrawable(mContext.getResources()
								.getDrawable(R.drawable.white_drawable));
					}
				}, mHeight,true);
			}
			if (album.getListPhotos().size() > 2) {
				holder.mImv3.setVisibility(View.VISIBLE);
				mImageLoader.DisplayImage(album.getListPhotos().get(2)
						.getImageUrl(), holder.mImv3, new OnLoadFileListner() {

					@Override
					public void onSuccess(boolean isAnimation) {
						// if (isAnimation) {
						// mImvPhoto.startAnimation(myFadeInAnimation);
						// }
					}

					@Override
					public void onStart() {
						// TODO Auto-generated method stub
						holder.mImv3.setImageDrawable(mContext.getResources()
								.getDrawable(R.drawable.white_drawable));
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						holder.mImv3.setImageDrawable(mContext.getResources()
								.getDrawable(R.drawable.white_drawable));
					}
				}, mHeight,true);
			}
			if (album.getListPhotos().size() > 3) {
				holder.mImv4.setVisibility(View.VISIBLE);
				mImageLoader.DisplayImage(album.getListPhotos().get(3)
						.getImageUrl(), holder.mImv4, new OnLoadFileListner() {

					@Override
					public void onSuccess(boolean isAnimation) {
						// if (isAnimation) {
						// mImvPhoto.startAnimation(myFadeInAnimation);
						// }
					}

					@Override
					public void onStart() {
						// TODO Auto-generated method stub
						holder.mImv4.setImageDrawable(mContext.getResources()
								.getDrawable(R.drawable.white_drawable));
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						holder.mImv4.setImageDrawable(mContext.getResources()
								.getDrawable(R.drawable.white_drawable));
					}
				}, mHeight,true);
			}
			holder.mTxvTitle.setText(album.getName() + " /"
					+ album.getListPhotos().size() + " Pictures");
			if (album.getListPhotos().size() == 1) {
				holder.mImv2.setVisibility(View.INVISIBLE);
				holder.mImv3.setVisibility(View.INVISIBLE);
				holder.mImv4.setVisibility(View.INVISIBLE);
			}
			if (album.getListPhotos().size() == 0) {
				holder.mImv1.setVisibility(View.INVISIBLE);
				holder.mImv2.setVisibility(View.INVISIBLE);
				holder.mImv3.setVisibility(View.INVISIBLE);
				holder.mImv4.setVisibility(View.INVISIBLE);
			}
			if (album.getListPhotos().size() == 2) {
				holder.mImv3.setVisibility(View.INVISIBLE);
				holder.mImv4.setVisibility(View.INVISIBLE);
			}
			if (album.getListPhotos().size() == 3) {
				holder.mImv4.setVisibility(View.INVISIBLE);
			}
		}

		return convertView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListAlbum.size();
	}

	public static class ViewHolder {
		private TextViewPlus mTxvTitle;
		private ImageView mImv1;
		private ImageView mImv2;
		private ImageView mImv3;
		private ImageView mImv4;
	}
}
